﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using Common.Model;
using Common.Util;
using Dapper.Contrib.Extensions;
using Newtonsoft.Json.Linq;
using pinduoduo.Model;
using pinduoduo.Util;
using RestSharp;
using ThumbnailSharp;
using Tk.Soc.Share.Common.Helpers;

namespace pinduoduo
{
    public class PddApi
    {

        //public readonly static string loginUrl= "https://api.pinduoduo.com/login";//登陆login

        //public readonly static string goodsDetailsUrl = "https://mobile.yangkeduo.com/proxy/api/api/galen/v2/goods/%d";//获取商品详情

        //public readonly static string userAddressUrl = "https://api.yangkeduo.com/api/origenes/address";//获取用户地址信息

        //public readonly static string shopDetailsUrl = "https://api.pinduoduo.com/mall/%d/info";//获取店铺信息

        //public readonly static string regionUrl = "https://mobile.yangkeduo.com/proxy/api/api/galen/v2/regions/%d?pdduid=%d";//获取省市区地址

        //public readonly static string createOrderUrl = "https://mobile.yangkeduo.com/proxy/api/order?pdduid=%d";//下单

        //public readonly static string payUrl = "https://mobile.yangkeduo.com/proxy/api/order/prepay?pdduid=%d";//支付宝url

        //public static string payWeChatUrl = "https://mobile.yangkeduo.com/friend_pay.html?fp_id=%s";//微信代付
        //public static string payWeChatH5Url = "/pay/wePayH5/%d";//微信h5支付

        //public static string orderListUrl = "https://mobile.yangkeduo.com/proxy/api/api/aristotle/order_list?pdduid=%d";//订单列表

        //public static $orderDetailsUrl = "https://mobile.yangkeduo.com/proxy/api/order/%s/received?pdduid=%d";//订单详情
        //public static $orderDetailsUrl = "https://mobile.yangkeduo.com/order.html?order_sn=%s";//订单详情

        //public static $pay = "https://mobile.yangkeduo.com/proxy/api/order/prepay?pdduid=%d";//支付url

        public static async Task<bool> UpdateNickName(PddToken pddToken, string nickname, string daili)
        {
            string url = "https://api.pinduoduo.com/user/profile/update/nickname?pdduid=" + pddToken.pdd_user_id;
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);

            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("Referer", "Android");
            request.AddHeader("pdd-config", "V4:001.054200");

            request.AddJsonBody(new { nickname, commit_check = "try" });
            var response = await restclient.ExecuteAsync(request);
            bool result = false;
            try
            {
                result = response.Content.Contains("修改成功");
            }
            catch (Exception e)
            {
                LogHelper.Logger.Error("修改头像异常", e);
            }
            return result;

        }

        public static async Task<bool> UpdateAvatar(PddToken pddToken, string avatar, string daili)
        {
            string url = "https://api.pinduoduo.com/user/profile/update/avatar?pdduid=" + pddToken.pdd_user_id;
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("Referer", "Android");
            request.AddHeader("pdd-config", "V4:001.054200");

            request.AddJsonBody(new { avatar });

            var response = await restclient.ExecuteAsync(request);
            bool result = false;
            try
            {
                var jo = JObject.Parse(response.Content);
                result = !string.IsNullOrWhiteSpace(jo["url"].ToString());
            }
            catch (Exception e)
            {
                LogHelper.Logger.Error("修改头像异常", e);
            }

            return result;
        }

        public static string ChatWsUrl = "wss://ws.pinduoduo.com/?role=user&client=web&enable_bubble=true&access_token=";

        public static int millisecondsDelay = 5000;
        /**
         * 上传评论照片前的签名
         * 
         * @param content
         * @param imageUrl
         * @return
         */
        public static async Task<string> UploadSignature(PddToken pddToken)
        {
            string url = "http://mobile.yangkeduo.com/proxy/api/image/signature?" + pddToken.pdd_user_id;
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            //if (daili != null)
            //{
            //    restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            //}

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("Referer", "Android");
            request.AddHeader("pdd-config", "V4:001.054200");

            request.AddJsonBody(new { bucket_tag = "review_image" });
            var response = await restclient.ExecuteAsync(request);
            string signature = null;
            try
            {
                var jo = JObject.Parse(response.Content);
                signature = jo["signature"].ToString();
            }
            catch (Exception e)
            {
                LogHelper.Logger.Error("获取签名异常", e);
            }
            return signature;

        }

        public static async Task<Images> UploadImage(PddToken pddToken, string upload_sign, string base64, string daili)
        {

            string url = "https://file.yangkeduo.com/v2/store_image";

            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }
            restclient.RemoteCertificateValidationCallback =
                (sender, certificate, chain, sslPolicyErrors) => true;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("Referer", "Android");
            request.AddHeader("pdd-config", "V4:001.054200");

            request.AddJsonBody(new { upload_sign, image = base64 });

            var response = await restclient.ExecuteAsync(request);
            //string imageUrl = null;
            try
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var jo = JObject.Parse(response.Content);
                    var Images = JsonHelper.DeserializeObject<Images>(response.Content);
                    return Images;
                    //imageUrl = jo["url"].ToString();
                    //if (!string.IsNullOrWhiteSpace(imageUrl)) 
                    //{
                    //    return Images;
                    //}
                }


            }
            catch (Exception e)
            {
                LogHelper.Logger.Error("头像上传异常", e);
            }

            return null;
        }

        public static async Task<string> Signature(PddToken pddToken, string daili, string bucket_tag = "pddavatar")
        {
            //获取上传签名
            string url = "https://api.pinduoduo.com/image/signature?pdduid=" + pddToken.pdd_user_id;
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (!string.IsNullOrWhiteSpace(daili))
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("Referer", "Android");
            request.AddHeader("pdd-config", "V4:001.054200");

            request.AddJsonBody(new { bucket_tag });
            var response = await restclient.ExecuteAsync(request);
            string signature = null;
            try
            {
                var jo = JObject.Parse(response.Content);
                signature = jo["signature"].ToString();
            }
            catch (Exception e)
            {
                LogHelper.Logger.Error("获取签名异常", e);
            }

            return signature;
        }

        /// <summary>
        /// yangkeduo
        /// </summary>
        /// <param name="pddToken"></param>
        /// <param name="daili"></param>
        /// <param name="bucket_tag"></param>
        /// <returns></returns>
        public static async Task<string> Signature2(PddToken pddToken, string daili, string bucket_tag = "pdd_chat_image")
        {
            //获取上传签名
            string url = "https://mobile.yangkeduo.com/proxy/api/image/signature?pdduid=" + pddToken.pdd_user_id;
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (!string.IsNullOrWhiteSpace(daili))
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Referer", "https://mobile.yangkeduo.com/chat_detail.html");

            request.AddJsonBody(new { bucket_tag });
            var response = await restclient.ExecuteAsync(request);
            string signature = null;
            try
            {
                var jo = JObject.Parse(response.Content);
                signature = jo["signature"].ToString();
            }
            catch (Exception e)
            {
                LogHelper.Logger.Error("获取签名异常", e);
            }

            return signature;
        }

        public static async Task<Images> UploadChatImage(PddToken pddToken, string upload_sign, string base64, string daili)
        {

            string url = "https://file.pinduoduo.com/v2/store_image?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (!string.IsNullOrWhiteSpace(daili))
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }
            restclient.RemoteCertificateValidationCallback =
                (sender, certificate, chain, sslPolicyErrors) => true;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "file.pinduoduo.com");
            request.AddHeader("Referer", "https://mobile.yangkeduo.com/");

            request.AddJsonBody(new { upload_sign, image = base64 });

            var response = await restclient.ExecuteAsync(request);
            //string imageUrl = null;
            try
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var jo = JObject.Parse(response.Content);
                    var Images = JsonHelper.DeserializeObject<Images>(response.Content);
                    return Images;
                    //imageUrl = jo["url"].ToString();
                    //if (!string.IsNullOrWhiteSpace(imageUrl)) 
                    //{
                    //    return Images;
                    //}
                }


            }
            catch (Exception e)
            {
                LogHelper.Logger.Error("头像上传异常", e);
            }

            return null;
        }


        public static async Task<string> UploadAvatar(PddToken pddToken, string upload_sign, string base64, string daili)
        {

            string url = "https://file.pinduoduo.com/store_avatar_image";
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("Referer", "Android");
            request.AddHeader("pdd-config", "V4:001.054200");

            request.AddJsonBody(new { upload_sign, image = base64 });

            var response = await restclient.ExecuteAsync(request);
            string imageUrl = null;
            try
            {
                var jo = JObject.Parse(response.Content);
                imageUrl = jo["url"].ToString();
            }
            catch (Exception e)
            {
                LogHelper.Logger.Error("头像上传异常", e);
            }

            return imageUrl;
        }

        public static Task<IRestResponse> GetPddMyInfo(string pdduid, string PDDAccessToken, string daili = null)
        {
            string url = "https://api.yangkeduo.com/user/profile/me?pdduid=" + pdduid;
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.GET);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", PDDAccessToken);
            request.AddHeader("Host", "apiv4.yangkeduo.com");

            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> GetGoodsComments(string goodId, string PDDAccessToken, string daili = null)
        {
            string url = "http://mobile.yangkeduo.com/goods_comments.html?goods_id=" + goodId;
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.GET);
            //request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", PDDAccessToken);
            request.AddHeader("Host", "mobile.yangkeduo.com");

            return restclient.ExecuteAsync(request);
        }


        public static Task<IRestResponse> FavoriteGood(PddToken pddToken, string goodId, string daili = null)
        {
            string url = "http://apiv3.yangkeduo.com/v2/favorite/like/" + goodId + "?pdduid=" + pddToken.pdd_user_id;
            var restclient = RestClientUtil.CreateRestClient(url);
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }
            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "apiv3.yangkeduo.com");
            request.AddHeader("Referer", url);

            request.AddObject(new { coupon_status = 1 });

            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> FavoriteMall(PddToken pddToken, string anti_content, string mallId, string daili = null)
        {
            string url = "http://yangkeduo.com/proxy/api/favorite/mall/like/" + mallId + "?pdduid=" + pddToken.pdd_user_id;
            var restclient = RestClientUtil.CreateRestClient(url);
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }
            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "yangkeduo.com");
            request.AddHeader("Referer", url);

            request.AddObject(new { anti_content, like_from = 101001 });

            return restclient.ExecuteAsync(request);
        }

        public static long GetPddTime()
        {
            var restclient = RestClientUtil.CreateRestClient("http://mobile.yangkeduo.com/proxy/api/api/server/_stm?pdduid=0");

            restclient.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36";

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            var response = restclient.Execute(request);

            if (response.Content.Contains("server_time"))
            {
                var jo = JObject.Parse(response.Content);
                return long.Parse(jo.GetValue("server_time").ToString());
            }
            return long.Parse(TimeHelp.GetTimeStamp(DateTime.Now));
        }

        public static Task<IRestResponse> PddOrderPrepay(PddToken pddToken, PDDOrderPrepay pddOrderPrepay, string daili)
        {
            string ur1 = "https://mobile.yangkeduo.com/proxy/api/order/prepay?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Referer", "");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pddOrderPrepay);
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> PddOrder(string pdduid, string PDDAccessToken, PDDOrder pddOrder, string source, string daili)
        {
            string ur1 = "http://apiv4.yangkeduo.com/order?pdduid=" + pdduid;
            string Referer = "http://mobile.yangkeduo.com/search_result.html?search_key=" + source + "&source=search&options=1&search_met_track=hot&refer_search_met_pos=0&refer_page_el_sn=615206&bui_search_id=" + pddOrder.group_id + "&refer_page_name=search&item_index=0&sp=0&is_back=1";
            Referer = HttpUtility.UrlEncode(Referer);

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("Referer", Referer);
            request.AddHeader("AccessToken", PDDAccessToken);

            request.AddJsonBody(pddOrder);
            return restclient.ExecuteAsync(request);
        }

        /// <summary>
        /// 第二种下单方式
        /// </summary>
        /// <param name="pdduid"></param>
        /// <param name="PDDAccessToken"></param>
        /// <param name="pddOrder"></param>
        /// <param name="source"></param>
        /// <param name="daili"></param>
        /// <returns></returns>
        public static Task<IRestResponse> PddOrderDuoDuo(PddToken pddToken, DuoDuoOrder pddOrder, string daili)
        {

            string ur1 = "https://mobile.yangkeduo.com/proxy/api/order?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("PDD-CONFIG", "V4:001.052300");
            request.AddHeader("p-appname", "pinduoduo");
            request.AddHeader("CLIENT_IP", daili);
            request.AddHeader("REMOTE_ADDR", daili);
            request.AddHeader("Via", daili);
            request.AddHeader("X-Forwarded-For", daili);

            request.AddHeader("Referer", ur1);
            request.AddHeader("X-PDD-QUERIES", "width=1080&height=1920&net=1&brand=xiaomi&model=MI+5X&osv=7.1.2&appv=4.33.0&pl=2");

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pddOrder);
            return restclient.ExecuteAsync(request);

        }

        public static Task<IRestResponse> PddOrderDuoDuo(PddToken pddToken, DuoDuoOrderNew pddOrder, string daili)
        {

            string ur1 = "https://mobile.yangkeduo.com/proxy/api/order?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("PDD-CONFIG", "V4:001.052300");
            request.AddHeader("p-appname", "pinduoduo");
            request.AddHeader("CLIENT_IP", daili);
            request.AddHeader("REMOTE_ADDR", daili);
            request.AddHeader("Via", daili);
            request.AddHeader("X-Forwarded-For", daili);

            request.AddHeader("Referer", ur1);
            request.AddHeader("X-PDD-QUERIES", "width=1080&height=1920&net=1&brand=xiaomi&model=MI+5X&osv=7.1.2&appv=4.33.0&pl=2");

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pddOrder);
            return restclient.ExecuteAsync(request);

        }


        /// <summary>
        /// 第二种下单方式
        /// </summary>
        /// <param name="pdduid"></param>
        /// <param name="PDDAccessToken"></param>
        /// <param name="pddOrder"></param>
        /// <param name="source"></param>
        /// <param name="daili"></param>
        /// <returns></returns>
        public static Task<IRestResponse> PddOrderPiNeiGou(PddToken pddToken, object pddOrder, string daili)
        {

            string ur1 = "https://mobile.yangkeduo.com/proxy/api/order?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("PDD-CONFIG", "V4:001.052300");
            request.AddHeader("p-appname", "pinduoduo");
            request.AddHeader("CLIENT_IP", daili);
            request.AddHeader("REMOTE_ADDR", daili);
            request.AddHeader("Via", daili);
            request.AddHeader("X-Forwarded-For", daili);

            request.AddHeader("Referer", ur1);
            request.AddHeader("X-PDD-QUERIES", "width=1080&height=1920&net=1&brand=xiaomi&model=MI+5X&osv=7.1.2&appv=4.33.0&pl=2");

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pddOrder);
            return restclient.ExecuteAsync(request);

        }
        //http://yangkeduo.com/proxy/api/api/vancouver/wait_for_shipping_update_address?pdduid=5140735937461
        //付款成功 修改地址
        public static Task<IRestResponse> UpdateAddress2(PddToken pddToken, string address_id, string order_sn, string daili)
        {

            string url = "http://yangkeduo.com/proxy/api/api/vancouver/wait_for_shipping_update_address?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(url, daili);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";


            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "yangkeduo.com");
            request.AddHeader("Referer", url);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(new { order_sn, address_id });
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> UpdateAddress(PddToken pddToken, string address_id, string order_sn, string daili)
        {

            //string url = "http://yangkeduo.com/proxy/api/api/vancouver/update_address?pdduid="+pddToken.pdd_user_id;
            //{"order_sn":"210807-058237962883019","address_id":"23449398554"}
            //http://yangkeduo.com
            //  "bucket_tag" : "review_video"

            string ur1 = "http://yangkeduo.com/proxy/api/api/vancouver/update_address?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(ur1, daili);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";


            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(new { order_sn, address_id });
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> AddAddress(PddToken pddToken, PddAddress pddAddress, string daili)
        {
            string ur1 = "http://apiv4.yangkeduo.com/address?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pddAddress);
            return restclient.ExecuteAsync(request);
        }


        public static Task<IRestResponse> GetPinNeigouGoodInfo(PinNeigou pinNeigou, PddToken pddToken)
        {
            string url = "https://api.pinduoduo.com/api/brand-philips-trade/goods/detail/v2?xcx_version=1.0.66";
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";


            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("Referer", url);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pinNeigou);
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> SetAddress(PddToken pddToken, PddAddress pddAddress, string daili)
        {
            string ur1 = "http://apiv4.yangkeduo.com/address?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pddAddress);
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> Leibniz(PddToken pddToken, string goodId, string daili)
        {
            string ur1 = "http://apiv4.yangkeduo.com/api/leibniz/goods/user/groups?pdduid=";

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("p-appname", "pinduoduo");
            request.AddHeader("PDD-CONFIG", "V4:001.052300");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(new { goods_id = goodId });
            return restclient.ExecuteAsync(request);
        }

        public static async Task<IEnumerable<PddAddress>> GetPddMyAddress(string pdduid, string PDDAccessToken, string daili = null)
        {
            string url = "https://api.yangkeduo.com/api/origenes/addresses";
            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }
            restclient.Timeout = 5000;
            var request = new RestRequest(Method.GET);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", PDDAccessToken);
            request.AddHeader("Host", "apiv.yangkeduo.com");
            var res = await restclient.ExecuteAsync(request);
            if (res.StatusCode == HttpStatusCode.OK)
            {
                try
                {
                    return JsonHelper.DeserializeObject<IEnumerable<PddAddress>>(res.Content);
                }
                catch (Exception e)
                {
                    LogHelper.Logger.Error("地址获取错误", e);
                }

            }
            return null;
        }


        public static Task<IRestResponse> AddAddressNew(PddToken pddToken, PddAddress pddAddress, string daili)
        {

            string ur1 = "http://apiv3.yangkeduo.com/address?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(ur1);
            restclient.Timeout = 5000;
            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv3.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pddAddress);
            return restclient.ExecuteAsync(request);
        }




        public static async Task<IEnumerable<PddAddress>> GetAllAddress(PddToken pddToken, string daili = null)
        {

            string ur1 = "http://apiv4.yangkeduo.com/addresses?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            if (!string.IsNullOrWhiteSpace(daili))
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            var res = await restclient.ExecuteAsync(request);
            if (res.StatusCode == HttpStatusCode.OK)
            {
                return JsonHelper.DeserializeObject<IEnumerable<PddAddress>>(res.Content);
            }
            return null;
        }

        //删除自己的地址
        public static async Task<int> DelAllAddress(PddToken pddToken, IEnumerable<PddAddress> pddAddress, string daili = null)
        {
            //https://api.pinduoduo.com/api/origenes/address/delete/22336610388?default_id=&pdduid=2648714130

            if (pddAddress == null || pddAddress.ToList().Count < 2)
            {
                return -1;
            }


            int i = 0;
            int result = 0;
            int count = pddAddress.ToList().Count;
            foreach (var item in pddAddress)
            {
                string ur1 = "http://apiv4.yangkeduo.com/address/delete/" + item.address_id + "?pdduid=" + pddToken.pdd_user_id;
                var restclient = RestClientUtil.CreateRestClient(ur1);

                restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

                if (!string.IsNullOrWhiteSpace(daili))
                {
                    restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
                }

                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json;charset=UTF-8");
                request.AddHeader("Accept", "*/*;zh-cn");
                request.AddHeader("Accept-Encoding", "gzip, deflate");
                request.AddHeader("Host", "apiv4.yangkeduo.com");
                request.AddHeader("Referer", ur1);

                request.AddHeader("AccessToken", pddToken.PDDAccessToken);
                var res = await restclient.ExecuteAsync(request);
                if (res.StatusCode == HttpStatusCode.OK)
                {
                    result++;
                }
                else
                {
                    LogHelper.Logger.Debug("删除地址失败：" + res.Content);
                }

                //if (i + 2 >= count) 
                //{
                //    break;
                //}
                Thread.Sleep(100);
                i++;
            }
            return result;
        }

        public static async Task<bool> DelAddressById(PddToken pddToken, string address_id, string daili = null)
        {
            //https://api.pinduoduo.com/api/origenes/address/delete/22336610388?default_id=&pdduid=2648714130


            string ur1 = "http://apiv4.yangkeduo.com/address/delete/" + address_id + "?pdduid=" + pddToken.pdd_user_id;
            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            if (!string.IsNullOrWhiteSpace(daili))
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            var res = await restclient.ExecuteAsync(request);
            if (res.StatusCode == HttpStatusCode.OK)
            {
                return true;
            }
            else
            {
                LogHelper.Logger.Debug("删除地址失败：" + res.Content);
            }
            return false;
        }

        //http://yangkeduo.com/proxy/api/api/origenes/address/delete/22285788194?pdduid=1205024422773&anti_content=0apAfqnYIRGyj992x4UVRVqlICPCc_gU33bRW6qN1lM3zaGqAzknCyz-UgXINFIBKlNFyOqk-LTlVoblVrdbvqgBjaQg7g_u2g7njWx2gm6taqbcM86Y3WhubvF-nI31NWoiEGi9nNc8gxoNbtiWXSAjMrAejrvM1JNQ-Beoi1h8OWJGGQ8nrY3q0f6jYmhLMVzFO-zPs_PV0gliZMhyjw9Te05SH9sQqhpe6H_Um0GeKwCIx9aomyX3odFtQ6N6tX_A8WRwSLMlQmqsBtQSQ_WEjLARarWMviLmz6M-1aKU1Syu9MSS1VIR1kU9VBS99taPAO0aFyJhhj9f4G3CqiQy5biZfPg4TShmh7aNCui7640gkWqb0EJOHm1Jl2kzeDCXerySFfTeP87vkrA8aMjIvOmgHzEXr9Wyo8cXNOKZCvubydAGHu34dImQpwsA7wz-iWuIPhP7UuJaALc1bo1yh0Xo6xSnhqMrv8YVkepj3vSSpJxDJ-ytCswgTBg9BZkW6qFgamVnnIr5LTi9KY8Nt0prr74ehlgQzzIrehYj7uJT-we9inB2V-E2s6_wDE3HuXqO_YpTULvvHTeu48IffEintC3pfWXX_h61_ex94iGI90RWjD2x
        public static Task<IRestResponse> GetGoodInfo(string goodId, PddToken pddToken)
        {

            string url = "http://mobile.yangkeduo.com/proxy/api/api/oak/integration/render?pdduid=" + pddToken.pdd_user_id;
            var restclient = RestClientUtil.CreateRestClient(url);

            var request = new RestRequest(Method.POST);
            PddIntegration pddIntegration = new PddIntegration();
            long goodIdLong;
            long.TryParse(goodId,out goodIdLong);
            pddIntegration.goods_id = goodIdLong;
            pddIntegration.hostname = "mobile.yangkeduo.com";
            pddIntegration.page_from = 101;
            pddIntegration.page_version = 7;
            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 8.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            //request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/837.36 (KHTML, like Gecko) Chrome/82.0.3202.89 Safari/537.36");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Referer", url);

            request.AddObject(pddIntegration);
            //var response = restclient.Execute(request);
            return restclient.ExecuteAsync(request);

        }

        public static PddGood GetGoodInfo2(string goodId, PddToken pddToken)
        {

            string url = "http://api.yangkeduo.com/api/oakstc/v14/goods/" + goodId + "?goods_id=" + goodId + "&pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 8.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";

            //restclient.Proxy = RestClientUtil.CreateWebProxy(address);

            var request = new RestRequest(Method.GET);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "api.yangkeduo.com");
            request.AddHeader("Referer", url);
            var response = restclient.Execute(request);

            if (string.IsNullOrWhiteSpace(response.Content))
            {
                MessageBox.Show("获取数据为空！接口有问题啦");
                return null;
            }

            if (response.Content.Contains("请登录后再操作"))
            {
                MessageBox.Show("请登录后再操作");
                return null;
            }

            if (response.Content.Contains("verify_auth_token")
               && response.Content.Contains("error_code"))
            {
                MessageBox.Show("请输入验证码");
                return null;
            }

            if (response.Content.Contains("verify_auth_token")
              && response.Content.Contains("error_code"))
            {
                MessageBox.Show("请输入验证码");
                return null;
            }

            JObject jo = JObject.Parse(response.Content);
            dynamic skuObj = jo.GetValue("sku");
            PddGood pddGood = new PddGood();
            if (skuObj != null)
            {
                dynamic groupObj = jo["group"];
                pddGood.gID2 = groupObj[0]["group_id"] + "";
                pddGood.gID1 = groupObj[1]["group_id"] + "";
                pddGood.good_id = goodId;
                string skuJson = JsonHelper.SerializeObject(skuObj);

                pddGood.sku = skuJson;
                pddGood.type = 2;
            }
            else
            {
                MessageBox.Show("获取数据失败");
                LogHelper.Logger.Error("获去商品接口2错误" + response.Content);
                return null;
            }
            dynamic promotion_activity_id = jo.GetValue("promotion_activity_id");
            if (promotion_activity_id != null)
            {
                pddGood.atvid = promotion_activity_id + "";

                PddGood pdd = DataBaseHelper.SqLiteDBCon.Get<PddGood>(pddGood.good_id);
                if (pdd == null)
                {
                    long reslut = DataBaseHelper.SqLiteDBCon.Insert<PddGood>(pddGood);
                }
                else
                {
                    pddGood.SourceKeywords = pdd.SourceKeywords;
                    DataBaseHelper.SqLiteDBCon.Update<PddGood>(pddGood);
                }
                //MessageBox.Show("获取数据成功");
                return pdd;
            }
            else
            {
                MessageBox.Show("获取数据失败，可能是获取太频繁了");
                return null;
            }

        }

        public static async Task<bool> GetOrderStatus(Order order, PddToken pddToken)
        {

            string url = "http://mobile.yangkeduo.com/proxy/api/order/" + order.orderSn + "?pdduid=" + order.uid;

            var restclient = RestClientUtil.CreateRestClient(url);
            restclient.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36";

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Referer", url);
            request.AddHeader("Origin", "http://mobile.yangkeduo.com");

            request.AddCookie("PDDAccessToken", pddToken.PDDAccessToken);

            var response = await restclient.ExecuteAsync(request);

            if (response.StatusCode == HttpStatusCode.OK && !string.IsNullOrWhiteSpace(response.Content))
            {
                if (response.Content.Contains("\"need_redirect\":\"transac_combine_order.html?parent_order_sn=PO-"))
                {
                    order.orderState = "待支付";
                    return true;
                }
                var jo = JObject.Parse(response.Content);

                var express_infoToken = jo.SelectToken("express_info");
                var chat_status_prompt = jo["chat_status_prompt"].ToString();
                order.orderState = chat_status_prompt;
                order.mallName = jo["mall"]["mall_name"].ToString();
                var order_goods = jo["order_goods"].ToArray();
                if (order_goods.Length > 0)
                {
                    var goods_name = order_goods[0]["goods_name"].ToString();
                    var spec = order_goods[0]["spec"].ToString();
                    order.goodsName = goods_name;
                    order.skuName = spec;
                }
                if (express_infoToken != null)
                {
                    order.logisticsOrderNo = express_infoToken["tracking_number"].ToString();
                    order.logisticsCompany = express_infoToken["shipping_name"].ToString();
                    var traces = express_infoToken["traces"].ToArray();
                    if (traces != null && traces.Length > 0)
                    {
                        var trace = traces.FirstOrDefault();
                        string info = trace["info"].ToString();
                        string status = trace["status"].ToString();
                        string time = trace["time"].ToString();
                        order.last_update_time_logistics = time;
                        //if (status == "SIGN") 
                        //{
                        //}
                        order.logisticsInfo = info;
                    }

                }
                return true;
            }
            LogHelper.Logger.Debug("同步订单状态：" + response.Content);
            return false;
        }



        public static async Task<IRestResponse> GetOrderInfo(Order order, PddToken pddToken)
        {

            string url = "http://mobile.yangkeduo.com/proxy/api/order/" + order.orderSn + "?pdduid=" + order.uid;

            var restclient = RestClientUtil.CreateRestClient(url);
            restclient.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36";

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Referer", url);
            request.AddHeader("Origin", "http://mobile.yangkeduo.com");

            request.AddCookie("PDDAccessToken", pddToken.PDDAccessToken);

            //var response = await restclient.ExecuteAsync(request);
            return await restclient.ExecuteAsync(request);

            //if (response.StatusCode == HttpStatusCode.OK && !string.IsNullOrWhiteSpace(response.Content))
            //{
            //    if (response.Content.Contains("\"need_redirect\":\"transac_combine_order.html?parent_order_sn=PO-"))
            //    {
            //        order.orderState = "待支付";
            //        return true;
            //    }
            //    var jo = JObject.Parse(response.Content);

            //    var express_infoToken = jo.SelectToken("express_info");
            //    var chat_status_prompt = jo["chat_status_prompt"].ToString();
            //    order.orderState = chat_status_prompt;
            //    order.mallName = jo["mall"]["mall_name"].ToString();
            //    var order_goods = jo["order_goods"].ToArray();
            //    if (order_goods.Length > 0)
            //    {
            //        var goods_name = order_goods[0]["goods_name"].ToString();
            //        var spec = order_goods[0]["spec"].ToString();
            //        order.goodsName = goods_name;
            //        order.skuName = spec;
            //    }
            //    if (express_infoToken != null)
            //    {
            //        order.logisticsOrderNo = express_infoToken["tracking_number"].ToString();
            //        order.logisticsCompany = express_infoToken["shipping_name"].ToString();
            //        var traces = express_infoToken["traces"].ToArray();
            //        if (traces != null && traces.Length > 0)
            //        {
            //            var trace = traces.FirstOrDefault();
            //            string info = trace["info"].ToString();
            //            order.logisticsInfo = info;
            //        }

            //    }
            //    return true;
            //}
            //LogHelper.Logger.Debug("同步订单状态：" + response.Content);
            //return false;
        }

        public static PddGood PaserGoogInfo(IRestResponse response, string goodId)
        {
            JObject jo = JObject.Parse(response.Content);
            dynamic skuObj = jo.GetValue("sku");
            PddGood pddGood = new PddGood();
            string mall_id = jo["goods"]["mall_id"].ToString();
            pddGood.mall_id = mall_id;
            if (skuObj != null)
            {
                dynamic groupObj = jo["goods"]["group"];

                pddGood.gID2 = groupObj[0]["group_id"] + "";
                pddGood.gID1 = groupObj[1]["group_id"] + "";
                pddGood.good_id = goodId;
                string skuJson = JsonHelper.SerializeObject(skuObj);

                pddGood.sku = skuJson;
                pddGood.type = (int)PddGoodSource.PuTong;
            }
            else
            {
                MessageBox.Show("获取数据失败");
                return null;
            }
            dynamic activity_collection = jo.GetValue("activity_collection");
            var coupon_list = jo["promotion"]["events"]?["mall_coupons"]?["coupon_list"]?.ToArray();
            if (coupon_list != null)
            {
                pddGood.coupon_list = JsonHelper.SerializeObject(coupon_list);
            }


            if (activity_collection != null)
            {
                try
                {
                    var activity_id = activity_collection["activity"]["activity_id"];
                    pddGood.atvid = activity_id + "";
                }
                catch (Exception)
                {

                }

                PddGood pdd = DataBaseHelper.SqLiteDBCon.Get<PddGood>(pddGood.good_id);
                if (pdd == null)
                {
                    long reslut = DataBaseHelper.SqLiteDBCon.Insert<PddGood>(pddGood);
                }
                else
                {
                    pddGood.SourceKeywords = pdd.SourceKeywords;

                    DataBaseHelper.SqLiteDBCon.Update<PddGood>(pddGood);
                }
                //MessageBox.Show("获取数据成功");
                return pdd;
            }
            else
            {
                MessageBox.Show("获取数据失败，可能是获取太频繁了");
                return null;
            }
        }




        public static Task<IRestResponse> take_coupon_v2(PddToken pddToken, TakeCoupon takeCoupon,string anti_content,string daili = null)
        {
            string url = "https://api.pinduoduo.com/api/daenerys/take_coupon_v2?pdduid="+ pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("P-Mode", "1");
            request.AddHeader("Etagc", "Etag");
   
            request.AddHeader("Referer", url);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
   //         "anti_content" : "0aoAfxn5mOtoY9dVUrMNTfc8G1wTdT13-T-OrLS_1HS7VBssVgU__m-uKff3JpzGH_MrzsrtUuo3mGsiWJWqMmJIwe93oHmngNV0djn9rJ5CHivv9RF7OPESxHgxlLGHdd3ONPoivX3VVYYxIexEiKNyxyydZVf8cG1A__65_kKGFjFMJctbted94OPbT3YvayEiS9nvXVyM8Oi3GBSB82clNO5U_mZt558CU09gHIOJjz9pvT16PCHZ8YKlgO5la3FFQUZe9M8cXVr9TOSMGE1BRQEaZtk5wBYEEtXItLYhs91uUn4mkuWvgXNWm77NbVwTCNoR3EW2uhJHt3vLXhUS52c36Af6i6lauMNyAdcvHvPJFle5hTjLcdp7d18xoUFh192Eep5cqZGfr7hBAxZONWlS03e1WKg1LK8QCC6JYSRhuxBLorZ1H8yLvg_N7K8fulVOwhqWLcnfVFEyHYQJoki9WweA2T",
   //"couponSn" : "",
   //"goodsId" : "269055140012",
   //"jinbaoAnti" : 1629609882441,
   //"mallId" : 137510792,
   //"pid" : "20929712_208780123",
   //"sourceId" : 2,
   //"sourceType" : 0
            //            {
            //                "anti_content" : "0aoAfxn5Hytbg9dVzRWl41JmAOB4KBVwkydl5NzMfztll4YSxZY9on0fk-EUgKvc18EQxcWaSWa1kEgFLlEaW8lb5cmlaC90nL2oydllqZ86MZRgu3jmFQPJ3x8y-VYZSWOy4sWlkMPRZ72lwaZd83OIlylWDryzkYU7TINyXZZnqT8IkRXd3yKevHsHPz1r-2OjJlnvN50zcJBEm1ZngYZ6Cmfp3G5P2MsSqp-mmCbVojH8Q9-o03jZUwKzEKTK5qPVDHwErqyJx05Pb3zETKWmfSQOPcvmGBlZxU4oCxOyBlq1Kd_WUoeBdb17e5bXLFbP9B5L2ceRfoUYTgb3qSFfqtLI1YWVxmYpX5oihh0iXmwd7d5xe7XB8n9G11ekkvRoZ-56RRpC6sxGYSa1qNImPpGbis5uLMT8zj-cJhFJLwEkQn65VvGWOyA8vv_ZFqmMXjDsiQH8h3RhEkqgVxLRSlZVLise715",
            //   "batch_sn" : "A0054VC-590393775152547292",
            //   "merchant_tag" : "4001"
            //}

            request.AddJsonBody(takeCoupon);
            return restclient.ExecuteAsync(request);
        }

        // http://mobile.yangkeduo.com/proxy/api/api/promotion/auto_take_merchant_coupon?pdduid=6878012836386&is_back=1
        public static Task<IRestResponse> AutoTakeMerchantCoupon(PddToken pddToken, string anti_content, string batch_sn, string daili = null)
        {
            string ur1 = "http://mobile.yangkeduo.com/proxy/api/api/promotion/auto_take_merchant_coupon?pdduid=6878012836386&is_back=1";

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Origin", "http://mobile.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            //            {
            //                "anti_content" : "0aoAfxn5Hytbg9dVzRWl41JmAOB4KBVwkydl5NzMfztll4YSxZY9on0fk-EUgKvc18EQxcWaSWa1kEgFLlEaW8lb5cmlaC90nL2oydllqZ86MZRgu3jmFQPJ3x8y-VYZSWOy4sWlkMPRZ72lwaZd83OIlylWDryzkYU7TINyXZZnqT8IkRXd3yKevHsHPz1r-2OjJlnvN50zcJBEm1ZngYZ6Cmfp3G5P2MsSqp-mmCbVojH8Q9-o03jZUwKzEKTK5qPVDHwErqyJx05Pb3zETKWmfSQOPcvmGBlZxU4oCxOyBlq1Kd_WUoeBdb17e5bXLFbP9B5L2ceRfoUYTgb3qSFfqtLI1YWVxmYpX5oihh0iXmwd7d5xe7XB8n9G11ekkvRoZ-56RRpC6sxGYSa1qNImPpGbis5uLMT8zj-cJhFJLwEkQn65VvGWOyA8vv_ZFqmMXjDsiQH8h3RhEkqgVxLRSlZVLise715",
            //   "batch_sn" : "A0054VC-590393775152547292",
            //   "merchant_tag" : "4001"
            //}

            request.AddJsonBody(new { anti_content, batch_sn, merchant_tag = "4001" });
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> GetRegions(PddToken pddToken, int regions = 1, string daili = null)
        {
            string url = "http://apiv3.yangkeduo.com/api/galen/v2/regions/" + regions + "?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 7.1.1; 1605-A01 Build/NMF26F; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.82 Mobile Safari/537.36  phh_android_version/4.5.1 phh_android_build/228842 phh_android_channel/pp";
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.GET);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json, text/javascript, */*");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Host", "apiv3.yangkeduo.com");

            return restclient.ExecuteAsync(request);
        }

        //退货
        public static Task<IRestResponse> AfterSales(PddToken pddToken, AfterSales afterSales, string daili = null)
        {

            string ur1 = "http://apiv4.yangkeduo.com/after_sales/create";

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);


            request.AddJsonBody(afterSales);
            return restclient.ExecuteAsync(request);
        }


        public static Task<IRestResponse> AsmCouponCheck(PddToken pddToken, string orderSn, string daili = null)
        {

            string ur1 = "http://apiv4.yangkeduo.com/api/asm/coupon/check";

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);


            request.AddJsonBody(new { order_sn = orderSn });
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> AfterSalesTrigger(PddToken pddToken, string orderSn, string daili = null)
        {

            string ur1 = "http://apiv4.yangkeduo.com/after_sales/trigger/" + orderSn;

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.GET);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> AfterSalesAll(PddToken pddToken, string orderSn, string daili = null)
        {
            string ur1 = "http://apiv4.yangkeduo.com/after_sales/all/reason?order_sn=" + orderSn + "&after_sales_id=0";

            var restclient = RestClientUtil.CreateRestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            var request = new RestRequest(Method.GET);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "apiv4.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            return restclient.ExecuteAsync(request);
        }

        //   http://mobile.yangkeduo.com/proxy/api/order/goods/review/append

        public static Task<IRestResponse> ReviewAppend(PddToken pddToken, string orderSn, string comment, string anti_content, string daili = null)
        {
            string ur1 = "http://mobile.yangkeduo.com/proxy/api/order/goods/review/append";

            var restclient = RestClientUtil.CreateRestClient(ur1, daili);

            restclient.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1";

            PddOrderReview pddOrderReview = new PddOrderReview();
            pddOrderReview.anonymous = 0;
            pddOrderReview.timeline_sync_type = 1;
            pddOrderReview.review_source = 1;
            pddOrderReview.comment = comment;
            pddOrderReview.order_sn = orderSn;
            pddOrderReview.pictures = new List<string>();
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Origin", "http://mobile.yangkeduo.com");
            request.AddHeader("Referer", ur1);

            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pddOrderReview);

            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> VideoReceived(PddToken pddToken,  Order order,   string anti_content, string daili = null)
        {
            string url = "https://api.pinduoduo.com/order/" + order.orderSn + "/received?pdduid=" + pddToken.pdd_user_id + "&new_share_code=1";

            var restclient = RestClientUtil.CreateRestClient(url, daili);

            restclient.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1";

            var request = new RestRequest(Method.POST);

            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("PDD-CONFIG", "V4:002.057800");
            request.AddHeader("p-appname", "pinduoduo");
            request.AddHeader("lat", "TKULUA6ZKVBWSLZJAEWWLYLZA7CDCVSF5LDON7GGYQO6ZUHX44UQ112116c");
            request.AddHeader("x-b3-ptracer", "EB1A33EF151A49B3B5599B1BEDEDC42D");
            request.AddHeader("anti-token", anti_content);
            request.AddHeader("rctk-sign", "K+cPJ2b9WsPriCkNzxHd8uM2B220Mu2SlLlmUiK2Yok=");
            request.AddHeader("rctk", "com.xunmeng.pinduoduo.ios&rctk_ver=5.78.0&rctk_ts=1630826274974&rctk_nonce=F03E6FC8DE6A45ACA9296BF63EFAB2F9&rctk_rpkg=0");
            request.AddHeader("X-PDD-QUERIES", "width=1242.000000&height=2208.000000&brand=apple&model=iPhone 7 Plus&osv=14.4.2&appv=5.78.0&pl=iOS&net=1");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            //GoTakeApicture goTakeApicture = new GoTakeApicture();
            //  comment_url = "pdd_comment_camera.html?goods_id=111396699669&order_sn=210907-294974965331553&goods_name=8%E5%8D%B7+%E8%AD%A6%E7%A4%BA%E8%AF%AD%E5%BF%AB%E9%80%92%E8%83%B6%E5%B8%A6%E9%AB%98%E7%B2%98%E5%BA%A6%E9%80%8F%E6%98%8E%E8%83%B6%E5%B8%A6%E5%A4%A7%E5%8D%B7%E5%B0%81%E7%AE%B1%E5%B0%81%E5%8F%A3%E6%89%93%E5%8C%85%E5%AE%BD%E8%83%B6%E5%B8%A6%E7%BA%B8%E6%89%B9%E5%8F%91&review_source=1&is_additional=false&pr_navigation_type=1&thumb_url=http%3A%2F%2Ft00img.yangkeduo.com%2Fgoods%2Fimages%2F2020-04-22%2F3c27426bb57af779dbaa150fd0f6cddf.jpeg&cate1_id=2603";

            //goTakeApicture.comment_url= "pdd_comment_camera.html?goods_id="+ order.goodsId+ "&order_sn="+ order.orderSn + "&goods_name="+ order.goodsName+ "&review_source=1&is_additional=false&pr_navigation_type=1&thumb_url=&cate1_id=";
            request.AddJsonBody(new { source  = "APP_ORDER_LIST" });
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> UploadVideo(byte[] videoBytes, PddToken pddToken, string daili = null)
        {
            string url = "https://file.pinduoduo.com/v2/general_file?sdk_version=ios-5.79.0&tag_name=review_video";
           
            var restclient = RestClientUtil.CreateRestClient(url, daili);

            restclient.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1";

            var request = new RestRequest(Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Host", "file.pinduoduo.com");
            //request.AddHeader("Referer", "http://file.pinduoduo.com/general_file");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddFile("file", videoBytes, "multipart/form-data");

            request.AddParameter("bucket_tag", "review_video", ParameterType.RequestBody);
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> GoodsReviewVideo(PddToken pddToken, PddOrderVideoReview pddOrderVideoReview, string anti_content, string daili = null) 
        {
            string url = "https://api.pinduoduo.com/v2/order/goods/review?pdduid=" + pddToken.pdd_user_id;

            var restclient = RestClientUtil.CreateRestClient(url, daili);

            restclient.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 ===  iOS/14.4.2 Model/iPhone9,2 BundleID/com.xunmeng.pinduoduo AppVersion/5.78.0 AppBuild/202108310029 pversion/2045 cURL/7.75.0";


            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "api.pinduoduo.com");
            request.AddHeader("PDD-CONFIG", "V4:002.057800");
            request.AddHeader("p-appname", "pinduoduo");
            request.AddHeader("lat", "TKULUA6ZKVBWSLZJAEWWLYLZA7CDCVSF5LDON7GGYQO6ZUHX44UQ112116c");
            request.AddHeader("x-b3-ptracer", "EB1A33EF151A49B3B5599B1BEDEDC42D");
            request.AddHeader("anti-token", anti_content);
            request.AddHeader("rctk-sign", "K+cPJ2b9WsPriCkNzxHd8uM2B220Mu2SlLlmUiK2Yok=");
            request.AddHeader("rctk", "com.xunmeng.pinduoduo.ios&rctk_ver=5.78.0&rctk_ts=1630826274974&rctk_nonce=F03E6FC8DE6A45ACA9296BF63EFAB2F9&rctk_rpkg=0");
            request.AddHeader("X-PDD-QUERIES", "width=1242.000000&height=2208.000000&brand=apple&model=iPhone 7 Plus&osv=14.4.2&appv=5.78.0&pl=iOS&net=1");
 
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pddOrderVideoReview);

            return restclient.ExecuteAsync(request);
        }


        public static async Task<string> SendText(ClientWebSocket webSocket, string message, string uid, string mallId)
        {
            CancellationToken cancellationToken = new CancellationTokenSource(millisecondsDelay).Token;
            string json = "{\"cmd\":\"send_message\",\"message\":{\"from\":{\"role\":\"user\",\"uid\":\"" + uid + "\"},\"to\":{\"role\":\"mall\",\"uid\":\"" + mallId + "\"},\"content\":\"" + message + "\",\"type\":0},\"version\":2,\"refer_page_name\":\"mall\",\"login_app_id\":\"\",\"anti_content\":\"0aoAfxnUOy1gYgdC4ApkP7AAtUCHSCf1D_LrLXRHY1Tj2tFHD7_Tsf2yHMcDGfUu-kqj_vnIMoT7r06rUFYtVkONtPoqPsqhcRXmBEk0T0C0N4gjaCrVcc14eKWPQZiBiOg6gqI1OPtCUi-MZRns-rnFgahVDJyzM4wRKa7vW6_m7PNqk_onyqnsZtKOw8I8fs3FZFL7c7xuvI1h6WsO--RpTe-8zkEu46tFvemRODkus4tvfwwozmZd5o7D71d1dwmOSMRpxvGp2J5VgZgQAkLy6nua04GzGYFt6g8fGJKIn_yYQMhCnLy2pS8ihAcEQx8cYmrst6X9j97xMUpO2Jjomb7r9a-J1-GTCib4Pm2oogt396j4_Ec4mG9aapHoG-e39X9MHnmz-BXBm74bJo_a-ngA2x6M6Q-gUtMsPnLm2U8F9FDDHfNAG-gJ0XSE_7dZub-dMG6Hb6v6ecb3Q7tlVyeBnMSFp7iXQDuQkOWsstXa7_EvEb69bh3vMWJDMpxWOi_jdfEDYzrY6hzJuxrU694eZlDVHZyXzGzldNb-W8wo0LmIG1pP5Fz8-ZKZO6s2P8EwgW3DlMKN7Ixm_9s3KXGLk7roO2yu7W32_qpqO8ig-52SCDC4ApySrQH64hHMoY-oil5jehpwgbEWNDIRTf93NA_\",\"user_agent\":\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36\",\"request_id\":8}";
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json);
            await webSocket.SendAsync(new ArraySegment<byte>(byteArray), WebSocketMessageType.Binary, true, cancellationToken);

            byte[] bufSize = new byte[4 * 1024];
            ArraySegment<byte> buf = new ArraySegment<byte>(bufSize);
            WebSocketReceiveResult r = await webSocket.ReceiveAsync(buf, cancellationToken);
            string msg = System.Text.Encoding.Default.GetString(bufSize);
            return msg;
        }

        public static async Task<string> SendImage(ClientWebSocket webSocket, string imageUrl, string uid, string mallId)
        {
            CancellationToken cancellationToken = new CancellationTokenSource(millisecondsDelay).Token;
            // string json = "{\"cmd\":\"send_message\",\"message\":{\"from\":{\"role\":\"user\",\"uid\":\""+uid+"\"},\"to\":{\"role\":\"mall\",\"uid\":\""+ mallId + "\"},\"content\":\""+ imageUrl + "\",\"type\":1},\"version\":2,\"refer_page_name\":\"mall\",\"login_app_id\":\"\",\"anti_content\":\"0aoAfxnUOy1gYgdC4ApkP7AAtUCHSCf1D_LrLXRHY1Tj2tFHD7_Tsf2yHMcDGfUu-kqj_vnIMoT7r06rUFYtVkONtPoqPsqhcRXmBEk0T0C0N4gjaCrVcc14eKWPQZiBiOg6gqI1OPtCUi-MZRns-rnFgahVDJyzM4wRKa7vW6_m7PNqk_onyqnsZtKOw8I8fs3FZFL7c7xuvI1h6WsO--RpTe-8zkEu46tFvemRODkus4tvfwwozmZd5o7D71d1dwmOSMRpxvGp2J5VgZgQAkLy6nua04GzGYFt6g8fGJKIn_yYQMhCnLy2pS8ihAcEQx8cYmrst6X9j97xMUpO2Jjomb7r9a-J1-GTCib4Pm2oogt396j4_Ec4mG9aapHoG-e39X9MHnmz-BXBm74bJo_a-ngA2x6M6Q-gUtMsPnLm2U8F9FDDHfNAG-gJ0XSE_7dZub-dMG6Hb6v6ecb3Q7tlVyeBnMSFp7iXQDuQkOWsstXa7_EvEb69bh3vMWJDMpxWOi_jdfEDYzrY6hzJuxrU694eZlDVHZyXzGzldNb-W8wo0LmIG1pP5Fz8-ZKZO6s2P8EwgW3DlMKN7Ixm_9s3KXGLk7roO2yu7W32_qpqO8ig-52SCDC4ApySrQH64hHMoY-oil5jehpwgbEWNDIRTf93NA_\",\"user_agent\":\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36\",\"request_id\":8}";
            string json = "{\"cmd\":\"send_message\",\"message\":{\"from\":{\"role\":\"user\",\"uid\":\"" + uid + "\"},\"to\":{\"role\":\"mall\",\"uid\":\"" + mallId + "\"},\"content\":\"" + imageUrl + "\",\"type\":1},\"version\":2,\"refer_page_name\":\"mall\",\"login_app_id\":\"\",\"anti_content\":\"0aoAfxnUOy1gYgdC4ApkP7AAtUCHSCf1D_LrLXRHY1Tj2tFHD7_Tsf2yHMcDGfUu-kqj_vnIMoT7r06rUFYtVkONtPoqPsqhcRXmBEk0T0C0N4gjaCrVcc14eKWPQZiBiOg6gqI1OPtCUi-MZRns-rnFgahVDJyzM4wRKa7vW6_m7PNqk_onyqnsZtKOw8I8fs3FZFL7c7xuvI1h6WsO--RpTe-8zkEu46tFvemRODkus4tvfwwozmZd5o7D71d1dwmOSMRpxvGp2J5VgZgQAkLy6nua04GzGYFt6g8fGJKIn_yYQMhCnLy2pS8ihAcEQx8cYmrst6X9j97xMUpO2Jjomb7r9a-J1-GTCib4Pm2oogt396j4_Ec4mG9aapHoG-e39X9MHnmz-BXBm74bJo_a-ngA2x6M6Q-gUtMsPnLm2U8F9FDDHfNAG-gJ0XSE_7dZub-dMG6Hb6v6ecb3Q7tlVyeBnMSFp7iXQDuQkOWsstXa7_EvEb69bh3vMWJDMpxWOi_jdfEDYzrY6hzJuxrU694eZlDVHZyXzGzldNb-W8wo0LmIG1pP5Fz8-ZKZO6s2P8EwgW3DlMKN7Ixm_9s3KXGLk7roO2yu7W32_qpqO8ig-52SCDC4ApySrQH64hHMoY-oil5jehpwgbEWNDIRTf93NA_\",\"user_agent\":\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36\",\"request_id\":8}";

            //  string json = "{\"cmd\":\"send_message\",\"message\":{\"from\":{\"role\":\"user\",\"uid\":\"1363852664863\"},\"to\":{\"role\":\"mall\",\"uid\":\"627671\"},\"content\":\"https://t00img.yangkeduo.com/chat/chat_user_upload/2021-05-18/9982935159702f1bed3ec1590f8c3fd9.jpeg\",\"type\":1},\"version\":2,\"refer_page_name\":\"mall\",\"login_app_id\":\"\",\"anti_content\":\"0aoAfxnUOy1gYgdC4ApkP7AAtUCHSCf1D_LrLXRHY1Tj2tFHD7_Tsf2yHMcDGfUu-kqj_vnIMoT7r06rUFYtVkONtPoqPsqhcRXmBEk0T0C0N4gjaCrVcc14eKWPQZiBiOg6gqI1OPtCUi-MZRns-rnFgahVDJyzM4wRKa7vW6_m7PNqk_onyqnsZtKOw8I8fs3FZFL7c7xuvI1h6WsO--RpTe-8zkEu46tFvemRODkus4tvfwwozmZd5o7D71d1dwmOSMRpxvGp2J5VgZgQAkLy6nua04GzGYFt6g8fGJKIn_yYQMhCnLy2pS8ihAcEQx8cYmrst6X9j97xMUpO2Jjomb7r9a-J1-GTCib4Pm2oogt396j4_Ec4mG9aapHoG-e39X9MHnmz-BXBm74bJo_a-ngA2x6M6Q-gUtMsPnLm2U8F9FDDHfNAG-gJ0XSE_7dZub-dMG6Hb6v6ecb3Q7tlVyeBnMSFp7iXQDuQkOWsstXa7_EvEb69bh3vMWJDMpxWOi_jdfEDYzrY6hzJuxrU694eZlDVHZyXzGzldNb-W8wo0LmIG1pP5Fz8-ZKZO6s2P8EwgW3DlMKN7Ixm_9s3KXGLk7roO2yu7W32_qpqO8ig-52SCDC4ApySrQH64hHMoY-oil5jehpwgbEWNDIRTf93NA_\",\"user_agent\":\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36\",\"request_id\":8}";
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json);
            await webSocket.SendAsync(new ArraySegment<byte>(byteArray), WebSocketMessageType.Binary, true, cancellationToken);

            byte[] bufSize = new byte[4 * 1024];
            ArraySegment<byte> buf = new ArraySegment<byte>(bufSize);
            WebSocketReceiveResult r = await webSocket.ReceiveAsync(buf, new CancellationTokenSource(millisecondsDelay).Token);
            string msg = System.Text.Encoding.UTF8.GetString(bufSize);
            return msg;
        }


        public static async Task<string> UploadAndSendImage(ClientWebSocket webSocket, string mallId, PddToken pddToken, string imageFile, string daili = "")
        {
            string upload_sign = await Signature(pddToken, daili, "pdd_chat_image");
            if (string.IsNullOrWhiteSpace(upload_sign))
            {
                upload_sign = await Signature2(pddToken, daili, "pdd_chat_image");
                if (string.IsNullOrWhiteSpace(upload_sign))
                {
                    return "图片获取签名异常！";
                }
            }
            string base64;
            try
            {
                var resultBytes = new ThumbnailCreator().CreateThumbnailBytes(
                                      thumbnailSize: 900,
                                      imageStream: new FileStream(imageFile, FileMode.Open, FileAccess.Read),
                                      imageFormat: Format.Jpeg
                                  );
                base64 = "data:image/jpg;base64," + Convert.ToBase64String(resultBytes);
            }
            catch (Exception e)
            {
                base64 = "data:image/jpg;base64," + ImageUtil.ImgToBase64String(imageFile);
            }

            var image = await UploadChatImage(pddToken, upload_sign, base64, daili);
            if (image != null)
            {
                return await SendImage(webSocket, image.url, pddToken.pdd_user_id, mallId);
            }
            return "";
        }

        public static async Task<bool> ChatWsIsAuth(ClientWebSocket webSocket)
        {
            if (webSocket.State != WebSocketState.Open)
            {
                return false;
            }
            byte[] bufSize = new byte[1 * 1024];
            ArraySegment<byte> buf = new ArraySegment<byte>(bufSize);
            WebSocketReceiveResult r = await webSocket.ReceiveAsync(buf, new CancellationTokenSource(millisecondsDelay).Token);
            string str = System.Text.Encoding.Default.GetString(bufSize);

            //{"auth":{"result":"ok"},"request_id":"1621323925","response":"auth","result":"ok","server_time":1621323925704,"status":1,"uid":"1363852664863"}
            var jo = JObject.Parse(str);
            var result = jo["auth"]["result"].ToString();

            if (result != "ok")
            {
                return false;
            }
            return true;
        }

    }
}
